package com.elinshaw.threadlocal;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author Administrator
 */
public class ThreadLocalTest {

    public static void main(String[] args) {
        //设置初始值
        ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 10);
        IntStream.range(0, 10).forEach(i -> new Thread(() -> {
            threadLocal.set(i);
            System.out.println(Thread.currentThread() + " set i " + threadLocal.get());
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(Thread.currentThread() + " get i " + threadLocal.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start());
    }

}
